<template>
  <div class="mybox">
    <div style="margin-bottom: 15px">
      <el-button type="primary" @click="visibleFLag = true">新增</el-button>
    </div>
    <el-table stripe border :data="tableData" lazy style="width: 100%" row-key="menuId">
      <el-table-column label="名称" prop="menuName" width="180"></el-table-column>
      <el-table-column label="权限编码" prop="code"></el-table-column>
      <el-table-column  label="图标" prop="icon" width="180"></el-table-column>
      <el-table-column label="类型" prop="type" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 0" type="primary">目录</el-tag>
          <el-tag v-if="scope.row.type == 1" type="success">菜单</el-tag>
          <el-tag v-if="scope.row.type == 2" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="菜单URL" prop="path" width="200"></el-table-column>
      <el-table-column label="菜单组件" prop="component" width="150"></el-table-column>
      <el-table-column label="排序号" sortable prop="sort" width="100"></el-table-column>
      <el-table-column label="是否显示在左侧菜单" prop="isNav" width="150">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isNav" @change="upd(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="状态"  prop="status">
        <template slot-scope="scope" width="150">
          <el-tag type="primary" v-if="scope.row.status == 1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="是否确认删除当前数据" @confirm="del(scope.row)">
            <el-button type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="visibleFLag" @close="close" width="45%" title="菜单信息">
      <div>
        <el-form :model="menus" label-width="80px" :rules="rules" ref="form">
          <el-form-item label="上级菜单">
            <el-select v-model="menus.parentId" placeholder="请选择">
              <template v-for="(item,index) in tableData">
                <el-option :label="item.menuName" :value="item.menuId"></el-option>
                <template v-for="(data,i) in item.children">
                  <el-option :label="data.menuName" :value="data.menuId">
                    <span>&nbsp;-&nbsp;{{data.menuName}}</span>
                  </el-option>
                </template>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item prop="menuName" label="菜单名称">
            <el-input placeholder="请输入菜单名称" v-model="menus.menuName"></el-input>
          </el-form-item>
          <el-form-item prop="code" label="权限编码">
            <el-input placeholder="请输入权限编码" v-model="menus.code"></el-input>
          </el-form-item>
          <el-form-item prop="icon" label="图标">
            <el-popover
                   placement="bottom"
                   width="390"
                   v-model="popoverFlag"
                   trigger="click">
                   <el-input  slot="reference" readonly @click="iconSelFlag=true" placeholder="请输入ICON图标" v-model="menus.icon"></el-input>
                   <div class="iconList">
                     <span v-for="item in iconList"  style="font-size:20px">
                       <i  :key="item" :class="item"  @click="setIcon(item)" ></i>
                     </span>
                   </div>
               </el-popover>
          </el-form-item>
          <el-form-item label="菜单URL">
            <el-input placeholder="请输入菜单URL" v-model="menus.path"></el-input>
          </el-form-item>
          <el-form-item required label="菜单组件">
            <el-input placeholder="请输入菜单组件" v-model="menus.component"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio v-model="menus.type" :label="0">目录</el-radio>
            <el-radio v-model="menus.type" :label="1">菜单</el-radio>
            <el-radio v-model="menus.type" :label="2">按钮</el-radio>
          </el-form-item>
          <el-form-item label="状态" prop="type">
            <el-radio v-model="menus.status" :label="0">禁用</el-radio>
            <el-radio v-model="menus.status" :label="1">正常</el-radio>
          </el-form-item>
          <el-form-item label="左侧菜单" prop="isNav">
            <el-radio v-model="menus.isNav" :label="false">不显示</el-radio>
            <el-radio v-model="menus.isNav" :label="true">显示</el-radio>
          </el-form-item>
          <el-form-item label="排序号" prop="sort">
            <el-input-number :min="1" v-model="menus.sort"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" style="display: flex;justify-content: flex-end">
        <el-button type="default" style="width: 90px" @click="visibleFLag=false">取消</el-button>
        <el-button type="primary" style="width: 90px" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {iconList} from "@/util/iconList.js"
export default {
  name: "Menu",
  data() {
    return {
      visibleFLag:false,
      menus:{
        parentId:0,
        menuName:"",
        code:"",
        icon:"",
        path:"",
        component:"",
        type:0,
        status:1,
        sort:1,
        isNav:false
      },
      rules:{
        menuName:[{required:true,message:"请输入菜单名称",trigger:"blur"}],
        code:[{required:true,message:"请输入权限编码",trigger:"blur"}],
        icon:[{required:true,message:"请输入icon图标",trigger:"change"}],
        type:[{required:true,message:"请选择类型",trigger:"blur"}],
        isNav:[{required:true,message:"请选择是否现在在面板中",trigger:"blur"}],
        status:[{required:true,message:"请选择状态",trigger:"blur"}],
        sort:[{required:true,message:"请输入排序号",trigger:"blur"}],
      },
      tableData: [],
      iconList,
      popoverFlag:false
    }
  },
  methods:{
    submit(){
      this.$refs['form'].validate(flag => {
        if(flag){
          this.$post("/api/menu/save",this.menus).then(res => {
            if(res.code == 200){
              this.$message.success(this.menus.menuId?"更新成功":"添加成功")
              this.visibleFLag = false
              this.initPage()
              this.close()
            }else this.$message.error(res.msg)
          })
        }
      })
    },
    upd(data){
      this.$post("/api/menu/save",data).then(res => {
        if(res.code == 200){
          this.$message.success("更新成功")
        }else this.$message.error(res.msg)
      })
    },
    del(data){
      this.$post("/api/menu/delete/"+data.menuId).then(res => {
        if(res.code == 200){
          this.$message.success("删除成功")
          this.initPage()
        }else this.$message.warning(res.msg)
      })
    },
    initPage(){
      this.$get("/api/menu/list").then(res =>{
        if(res.code == 200)this.tableData = res.data
      })
    },
    edit(data){
      this.menus = JSON.parse(JSON.stringify(data))
      this.visibleFLag = true
    },
    close(){
      this.menus = {
        parentId:0,
        menuName:"",
        code:"",
        icon:"",
        path:"",
        component:"",
        type:0,
        status:1,
        sort:1
      }
    },
    setIcon(item){
      this.menus.icon=item
      this.popoverFlag=false
    }
  },
  created() {
    this.initPage()
  }
}
</script>

<style scoped lang="scss">
.mybox >>> .el-table__header,
.mybox >>> .el-table__body{
  height: 100%;
  margin:0;
}
.iconList{
    width: 400px;
    height: 300px;
    overflow-y: scroll;
    overflow-x: hidden;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    span{
        display: inline-block;
        width: 60px;
        height: 45px;
        color: #000000;
        font-size: 20px;
        border: 1px solid #E6E6E6;
        border-radius: 4px;
        cursor: pointer;
        text-align: center;
        line-height: 45px;
        margin: 5px ;
    }
}
</style>
